
import 'package:rapid_kit/rapid_kit.dart';
import '../base/composed_player.dart';


class LivePlayer extends ComposedPlayer {
  final Pipe _pipe;
  late LiveStream _streamPrimary;
  LiveStream? _streamSecondary;

  LivePlayer(this._pipe) : super();

  @override
  void prepare(int cameraCount) {
    super.prepare(cameraCount);

    _streamPrimary = LiveStream.createPrimary(_pipe);

    playerOfCamera0.prepare(_streamPrimary.provider()!);

    if (cameraCount == 2) {
      // Old multi-channel Camera use Secondary channel to transfer video data from caemra-1
      _streamSecondary = LiveStream.createSecondary(_pipe);
      playerOfCamera1?.prepare(_streamSecondary!.provider(cameraIndex: 1)!);
    }

    if (cameraCount >= 3) {
      // New multi-channel Camera use Promary channel to transfer all video data
      playerOfCamera1?.prepare(_streamPrimary.provider(cameraIndex: 1)!);
      playerOfCamera2?.prepare(_streamPrimary.provider(cameraIndex: 2)!);
    }
  }

  void switchQuality(int quality) {
    _streamPrimary.switchQuality(quality);
  }

  Future<List<Quality>?> listQualities() async {
    if (!_pipe.isEstablished) return null;
    final resp = await InstructStandard.quality(_pipe);
    return resp.success ? resp.data : null;
  }

  @override
  void resume() {
    super.resume();
    _streamPrimary.start();
    _streamSecondary?.start();
  }

  @override
  void pause() {
    super.pause();
    _streamPrimary.stop();
    _streamSecondary?.stop();
  }

  @override
  void dispose() {
    super.dispose();
    _streamPrimary.destroy();
    _streamSecondary?.destroy();
  }
}
